package com.banciyuan.bcywebview.biz.goods.store;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.ZhipinApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.biz.goods.shoppingcart.ShoppingCartActivity;
import com.banciyuan.bcywebview.biz.goods.shoppingcart.ShoppingCartHelper;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;
import de.greenrobot.daoexample.model.GoodsProduct;


/**
 * Created by squall on 2016/11/3.
 */

public class AllGoodsActivity extends BaseActivity implements View.OnClickListener {

    private TabLayout tabLayout;
    private ViewPager mViewpager;
    private View buy_car_num;
    private ImageView go_back;
    private FrameLayout goods_car;
    private int type;
    private List<Fragment> flist = new ArrayList<>();
    private List<GoodsProduct> goodsProducts = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_all_activity);
        initArgs();
        initUi();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        type = getIntent().getIntExtra(gotoUtil.INTENT_VALUE_ONE, 0);
    }

    private void getFragment() {
        for (int i = 0; i < goodsProducts.size(); i++) {
            AllGoodsFragment allGoodsFragment = new AllGoodsFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type", goodsProducts.get(i).getZp_id());
            allGoodsFragment.setArguments(bundle);
            flist.add(allGoodsFragment);
        }
    }

    @Override
    protected void initUi() {
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        goods_car = (FrameLayout) findViewById(R.id.goods_car);
        buy_car_num = findViewById(R.id.buy_car_num);
        go_back = (ImageView) findViewById(R.id.base_action_bar_home);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mViewpager = (ViewPager) findViewById(R.id.mViewpager);
    }

    @Override
    protected void initData() {
        String url = HttpUtils.BASE_URL + ZhipinApi.getListGoodsProducts();
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    if (StatusCodeUtils.dealStatusCode(response, AllGoodsActivity.this)) {
                        JSONObject jsonObject = new JSONObject(response);
                        Gson gson = new Gson();
                        goodsProducts.add(new GoodsProduct("", getString(R.string.all), 0));
                        List<GoodsProduct> tempGoodsProducts = gson.fromJson(jsonObject.getString(HttpUtils.UPLOAD_DATA), new TypeToken<List<GoodsProduct>>() {
                        }.getType());
                        goodsProducts.addAll(tempGoodsProducts);
                        dealGoodsProducts();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        };
        OnErrorProxy onErrorProxy = new OnErrorProxy(onError, onSucc, url, this, null);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, null, onSucc, onErrorProxy);
        VolleyQueue.getRquest(this).add(requestParam);
    }

    private void dealGoodsProducts() {
        getFragment();
        mViewpager.setOffscreenPageLimit(goodsProducts.size());
        mViewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return flist.get(position);
            }

            @Override
            public int getCount() {
                return goodsProducts.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return goodsProducts.get(position).getName();
            }
        });
        tabLayout.setupWithViewPager(mViewpager);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setCurrentProduct();
            }
        });
    }

    private void setCurrentProduct() {
        for (int i = 0; i < goodsProducts.size(); i++) {
            if (type == goodsProducts.get(i).getZp_id())
                mViewpager.setCurrentItem(i);
        }
    }

    @Override
    protected void initAction() {
        goods_car.setOnClickListener(this);
        go_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.goods_car:
                gotoUtil.gotoAct(this, ShoppingCartActivity.class);
                break;
            case R.id.base_action_bar_home:
                finish();
                break;
        }
    }

    private void readCar() {
        List<DetailGoods> dlist = ShoppingCartHelper.getShoppingItems(this);
        if (dlist.isEmpty()) {
            buy_car_num.setVisibility(View.GONE);
        } else {
            buy_car_num.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        readCar();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }
}
